package com.cy.pj.sys.controller;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.JsonResult;
import com.cy.pj.sys.pojo.SysMenu;
import com.cy.pj.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 张琪
 * @date 2020/11/14
 * @describe
 */
//指定访问路径, 使得程序请求可以直接进入

@RequestMapping("/menu/")
@RestController
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;



    @GetMapping("doFindObjects")
    public JsonResult doFindObjects(){
        List<Map<String, Object>> list = sysMenuService.findObjects();
        return new JsonResult(list);
    }

    @GetMapping("doFindZtreeMenuNodes")
    public JsonResult findZtreeMenuNodes(){
        return new JsonResult(sysMenuService.findZtreeMenuNodes());
    }

    @RequestMapping("doSaveObject")
    public JsonResult doSaveObject(SysMenu entity){
        int rows = sysMenuService.saveObject(entity);
        if(rows == 0)
            return new JsonResult ("save fault");
        return new JsonResult ("save ok");
    }

    @RequestMapping("doUpdateObject")
    public JsonResult doUpdateObject(SysMenu sysMenu){
        int rows = sysMenuService.updateObject(sysMenu);
        if(rows == 0)
            return new JsonResult ("update fault");
        return new JsonResult ("update ok");


    }

    @RequestMapping("doDeleteObject")
    public JsonResult doDeleteObject(Integer id){
        System.err.println("shibai");
        int rows = sysMenuService.deleteObject(id);
        if(rows == 0){
            throw new ServiceException("delete false");
        }
        return new JsonResult("delete success");
    }



}
